#include "user_server.hpp"
#include "base.pb.h"
#include "file.pb.h"
#include "log.hpp"
#include <gflags/gflags.h>

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "日志等级: 0-TRACE, 1-DEBUG, 2-INFO, 3-WARN, 4-ERROR, 5-FATAL");

DEFINE_string(mysql_host, "127.0.0.1", "Mysql服务器访问地址");
DEFINE_string(mysql_user, "root", "Mysql服务器访问用户名");
DEFINE_string(mysql_pswd, "123456", "Mysql服务器访问密码");
DEFINE_string(mysql_db, "chat_system", "Mysql默认库名称");
DEFINE_string(mysql_cset, "utf8", "Mysql客户端字符集");
DEFINE_int32(mysql_port, 3306, "Mysql服务器访问端口");
DEFINE_int32(mysql_pool_count, 4, "Mysql连接池最大连接数量");

DEFINE_string(redis_host, "127.0.0.1", "Redis服务器访问地址");
DEFINE_int32(redis_port, 6379, "Redis服务器访问端口");
DEFINE_int32(redis_db, 0, "Redis默认库号");
DEFINE_bool(redis_keep_alive, true, "Redis长连接保活选项");

DEFINE_string(es_host, "http://127.0.0.1:9200/", "ES搜索引擎服务器URL");


DEFINE_string(etcd_host, "http://127.0.0.1:2379", "etcd的地址");
DEFINE_string(base_service, "/service", "服务注册的根目录");
DEFINE_string(instance_name, "/user_service/instance", "服务实例名称");
DEFINE_string(service_host, "127.0.0.1:10003", "文件服务外部访问地址");
DEFINE_string(file_instance_name, "/file_service", "文件服务名称");

DEFINE_uint32(port, 10003, "rpc服务器端口号");
DEFINE_int32(timeout_sec, -1, "rpc调用的超时时间");
DEFINE_uint32(num_threads, 4, "rpc服务器工作线程数量");

DEFINE_string(dms_key_id, "LTAI5t6NF7vt499UeqYX6LB9", "短信平台密钥ID");
DEFINE_string(dms_key_secret, "5hx1qvpXHDKfQDk73aJs6j53Q8KcF2", "短信平台密钥");
int main(int argn, char* argv[])
{
    google::ParseCommandLineFlags(&argn, &argv, true);
    init_logger(FLAGS_log_file, int_log_level_map(FLAGS_log_level), FLAGS_run_mode);
    UserServerBuilder builder;
    LOG_DEBUG("{}", FLAGS_etcd_host);
    LOG_DEBUG("{}", FLAGS_es_host);
    builder.make_dms_object(FLAGS_dms_key_id, FLAGS_dms_key_secret);
    builder.make_discovery_object(FLAGS_etcd_host, FLAGS_base_service, FLAGS_file_instance_name);
    builder.make_mysql_object(FLAGS_mysql_user, FLAGS_mysql_pswd, FLAGS_mysql_host, 
    FLAGS_mysql_db, FLAGS_mysql_cset, FLAGS_mysql_port, FLAGS_mysql_pool_count);
    builder.make_redis_object(FLAGS_redis_host, FLAGS_redis_port, FLAGS_redis_db, FLAGS_redis_keep_alive);
    builder.make_es_object({FLAGS_es_host});
    builder.make_user_service_impl();
    builder.make_rpc_object(FLAGS_port, FLAGS_timeout_sec, FLAGS_num_threads);
    builder.make_reg_object(FLAGS_etcd_host, FLAGS_base_service + FLAGS_instance_name, FLAGS_service_host);
    UserServer::ptr user_server = builder.build();
    if(!user_server)
    {
        LOG_ERROR("创建用户管理服务失败");
        return -1;
    }
    user_server->start();

    return 0;
}